package com.example.su_shi_tan_home.child

import android.view.View
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.library_common.utils.normalShortToast
import com.example.su_shi_tan_bas_lib.base.BaseLazyDataBindingFragment
import com.example.su_shi_tan_bas_lib.global.AppGlobals
import com.example.su_shi_tan_bas_lib.networking.RestClient
import com.example.su_shi_tan_bas_lib.utils.even.MessageWrap
import com.example.su_shi_tan_bas_lib.utils.even.TO_REFRESH_USER_SIGN_USER_INFO
import com.example.su_shi_tan_core.arouter.HomeARouter
import com.example.su_shi_tan_core.arouter.WalletARouter
import com.example.su_shi_tan_core.arouter.WalletARouterParams
import com.example.su_shi_tan_core.constant.SignUserBeanHolder
import com.example.su_shi_tan_core.extentions.castBaseFragmentEx
import com.example.su_shi_tan_core.extentions.startNavigation
import com.example.su_shi_tan_core.getUserInfo
import com.example.su_shi_tan_home.R
import com.example.su_shi_tan_home.databinding.LightingFragmentBinding
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.interfaces.SimpleCallback

@Route(path = HomeARouter.LIGHTING)
class LightingFragment : BaseLazyDataBindingFragment<LightingFragmentBinding>(),
    View.OnClickListener {

    override fun layoutId(): Int {
        return R.layout.lighting_fragment
    }

    val pageLs = listOf(
        withARouter(HomeARouter.LightingAllFragment) to R.string.lighting_all,
        withARouter(HomeARouter.LightingTransOutFragment) to R.string.lighting_trans_out,
        withARouter(HomeARouter.LightingTransInFragment) to R.string.lighting_trans_in
    )

    private var mPagerAdapter: FragmentStatePagerAdapter? = null

    override fun initView(view: View) {
        val mToolbar: Toolbar = view.findViewById(R.id.mToolbar)
        mToolbar.title = "激活积分（LD)"
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }

        mPagerAdapter = object :
            FragmentStatePagerAdapter(childFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            override fun getCount(): Int {
                return pageLs.size
            }

            override fun getItem(position: Int): Fragment {
                return pageLs[position].first
            }

            override fun getPageTitle(position: Int): CharSequence? {
                return AppGlobals.getApplication().getString(pageLs[position].second)
            }
        }
        viewBinder.mViewpager.adapter = mPagerAdapter

        viewBinder.mTabLayout.setupWithViewPager(viewBinder.mViewpager)

        viewBinder.onViewClick=this
    }

    private fun withARouter(path: String) = ARouter.getInstance()
        .build(path)
        .castBaseFragmentEx()

    override fun lazyInit() {
        getUserInfo(_mActivity)
    }

    override fun handMessage(msg: MessageWrap?) {
//        super.handMessage(msg)
        when(msg?.message){
            TO_REFRESH_USER_SIGN_USER_INFO->{
                viewBinder.bean=SignUserBeanHolder.getSignUser()

            }
        }
    }


    override fun onBackPressedSupport(): Boolean {
        pop()
        return true
    }

    override fun onClick(v: View?) {
         when(v?.id){
             R.id.souKan->{
                 ARouter.getInstance()
                     .build(WalletARouter.CollectionsFragment)
                     .withString(WalletARouterParams.tokenType,WalletARouterParams.LD)
                     .startNavigation(this)
             }

             R.id.transfer->{
                 ARouter.getInstance()
                     .build(WalletARouter.TransferFragment)
                     .withString(WalletARouterParams.tokenType,WalletARouterParams.LD)
                     .startNavigation(this)
             }

             R.id.mActivation->{
                 val activationPop = ActivationPop(_mActivity)

                 XPopup.Builder(_mActivity)
                     .hasShadowBg(true)
                     .setPopupCallback(object : SimpleCallback() {
                         override fun onDismiss(popupView: BasePopupView?) {
                             super.onDismiss(popupView)
//                            getSignUser()
                             if (activationPop.clicked){
                                 sendActive()
                             }
//                             getUserInfo(_mActivity)
                         }
                     })
                     .asCustom(activationPop)
                     .show()

//                 showErrorPop()

//                 sendActive()

             }
         }
    }

    private fun sendActive() {

                 RestClient.builder()
                     .url(R.string.user_activation)
                     .success {
                         normalShortToast(_mActivity,"激活成功")
                         getUserInfo(_mActivity)
                     }
                     .failure {
                         normalShortToast(_mActivity,R.string.net_work_failed)

                     }.error { _, msg ->
//                         normalShortToast(_mActivity,msg)
                         showErrorPop()
                     }
                     .build().post()
    }

    private fun showErrorPop() {
        val activationErrorPop = ActivationErrorPop(_mActivity)
        XPopup.Builder(_mActivity)
            .hasShadowBg(true)
            .asCustom(activationErrorPop)
            .show()
    }


}